<?php

/**
 * @copyright   FOSP
 * @version     20120411
 * @todo        Rating player model
 * @name        RatingPlayer
 * @author      QuangTM 
 */
class RatingPlayer
{

    /**
     *
     * @var RatingPlayer
     */
    protected static $instance;

    protected function __construct()
    {
        
    }

    /**
     * Get RatingPlayer's instance
     * @return RatingPlayer
     * @author QuangTM
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    /**
     * add player who rated match
     * @param array $params
     * @return boolean 
     * @author QuangTM
     */
    public function addRatedPlayer($params)
    {
        try
        {
            // Get application config
            $config = Fpt_Global::getApplicationIni();

            // Get obj to access table rating_player
            $ratedPlayerMysqlObj = Fpt_Business_RatingPlayer::factory($config['database']['default']);
            $ratedPlayerRedisObj = Fpt_Business_RatingPlayer::factory($config['database']['nosql']);

            // Insert to mysql
            if ($ratedPlayerMysqlObj->addRatedPlayer($params) !== FALSE)
            {
                // Insert to redis
                $ratedPlayerRedisObj->addRatedPlayer($params);
                return TRUE;
            }
            return FALSE;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Check users participated in evaluating the match or not.
     * @param int $userID
     * @param int $matchID
     * @return boolean 
     * @author QuangTM
     */
    public function checkPlayerIsRatedMatch($userID, $matchID)
    {
        try
        {
            // Get application config
            $config = Fpt_Global::getApplicationIni();

            // Get obj to access table rating_player
            $ratedPlayerRedisObj = Fpt_Business_RatingPlayer::factory($config['database']['nosql']);
            
            // Check in redis SET
            if(!$ratedPlayerRedisObj->isRated($userID, $matchID))
            {
                // If not in redis SET, check again in MySQL to sure
                $ratedPlayerMysqlObj = Fpt_Business_RatingPlayer::factory($config['database']['default']);
                
                // If has in MySQL
                if ($ratedPlayerMysqlObj->isRated($userID, $matchID))
                {
                    // Push in to redis (redis miss this userid)
                    $ratedPlayerRedisObj->addRatedPlayer(array(
                        'MatchID' => $matchID,
                        'UserID'  => $userID
                    ));
                    return TRUE;
                }
                return FALSE;
            }
            return TRUE;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

}